var express = require("express");
var router = express.Router();
const fs = require("fs");
const path = require("path");
const { login } = require("../controller/user");

//登录页面
router.get("/login", (req, res, next) => {
    console.log(req.session);
    const filePath = path.join(__dirname, "../pages/login.html");
    if (fs.existsSync(filePath)) {
        fs.readFile(filePath, (err, data) => {
            if (err) {
                console.log(err);
            } else {
                res.send(data.toString());
            }
        })
    }
})
//登录验证
router.post("/login", (req, res, next) => {
    login(req.body).then((data) => {
        if (data.length > 0) {
            req.session.username = data[0].username;
            req.session.realname = data[0].realname;
            //保存session的修改（非常重要）
            req.session.save();
            console.log(req.session);
            console.log("登录成功");
        } else {
            console.log("登录失败");
        }
    })
})

module.exports = router;
